package com.example.lala.mvp.view.fragment;


import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.lala.R;
import com.example.lala.adapter.MoneyAdapter;
import com.example.lala.bean.Money;
import com.example.mylibrary.mvp.view.BaseFragment;
import com.google.gson.Gson;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Pig2Fragment extends BaseFragment {
    String url = "http://api.city2sky.cn/api/WeekTest/getProducts";
    private RecyclerView rv;
    private List<Money.DataBean> list = new ArrayList<>();
    MoneyAdapter moneyAdapter;
    private Button bt;
    private DrawerLayout dl;
    private TextView tv1;
    private TextView tv2;
    private EditText et;

    @Override
    public int bondLayout() {
        return R.layout.fragment_pig2;
    }

    @Override
    public void initView() {
        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        initData();  //数据

        bt = (Button) findViewById(R.id.bt);
        dl = (DrawerLayout) findViewById(R.id.dl);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        et = (EditText) findViewById(R.id.et);
    }

    @Override
    public void initData() {
        OkGo.<String>get(url)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body();
                        Money money = new Gson().fromJson(body, Money.class);
                        List<Money.DataBean> data = money.getData();
                        list.addAll(data);
                        moneyAdapter = new MoneyAdapter(R.layout.money_item, list);
                        rv.setAdapter(moneyAdapter);
                        //点击事件
                        moneyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                dl.openDrawer(Gravity.LEFT);
                                tv1.setText(list.get(position).getProductname());
                                tv2.setText(list.get(position).getYieldrate()+"");
                               // 校验金额不能为空而且必须为数字，金额不能大于5000
                                bt.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        String trim = et.getText().toString().trim();
                                        if(trim.isEmpty()){
                                            Toast.makeText(getContext(), "不能为空", Toast.LENGTH_SHORT).show();;
                                            return;
                                        }
                                        String s = et.getText().toString();
                                        Toast toast = Toast.makeText(getContext(), "购买成功", Toast.LENGTH_SHORT);
                                        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.pop, null);
                                        TextView textView = inflate.findViewById(R.id.tv);
                                        textView.setText("成功买入\n"+"恭喜：买入成功"+list.get(position).getProductname()+s+"元" );
                                        toast.setGravity(Gravity.TOP,0,0);
                                        toast.setView(inflate);
                                        toast.show();










                                        Notification.Builder builder = new Notification.Builder(getContext());
                                        builder.setPriority(Notification.PRIORITY_MAX);
                                        builder.setSmallIcon(R.mipmap.ic_launcher);
                                        builder.setContentTitle("成功买入\n"+"恭喜：买入成功"+list.get(position).getProductname()+s+"元");
                                        Notification build = builder.build();
                                        NotificationManager systemService = (NotificationManager)getActivity(). getSystemService(Context.NOTIFICATION_SERVICE);
                                        systemService.notify(0,build);

                                    }
                                });

                            }
                        });
                    }
                });
    }

    @Override
    public void Toast(String s) {

    }
}
